﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace iMall.AtoZ.Helper
{
    public class SqlDALHelper
    {
        /// <summary>
        /// 读取Sql数据库的连接字符串
        /// 首先从缓存里读取，如果不存在则到配置文件中读取，并放入缓存
        /// </summary>
        /// <returns>Sql数据库的连接字符串</returns>
        public static string GetConnectionString ( )
        {
            string connectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;

            return connectionString;
        }

        /// <summary>
        /// 执行SQL语句并且不返回任何值
        /// </summary>
        /// <param name="SQLCommand">所执行的SQL命令</param>
        /// <param name="parameters">参数集合</param>
        public static int ExecuteSQLNonQuery ( string SQLCommand, SqlParameter[] parameters )
        {
            using ( SqlConnection connection = new SqlConnection(GetConnectionString()) )
            {
                SqlCommand command = new SqlCommand(SQLCommand, connection);
                for ( int i = 0; i < parameters.Length; i++ )
                    command.Parameters.Add(parameters[i]);
                connection.Open();
                int re = command.ExecuteNonQuery();
                connection.Close();

                return re;
            }
        }

        /// <summary>
        /// 执行SQL语句并返回包含查询结果的DataSet
        /// </summary>
        /// <param name="SQLCommand">所执行的SQL命令</param>
        /// <param name="parameters">参数集合</param>
        /// <returns></returns>
        public static DataSet ExecuteSQLDataSet ( string SQLCommand )
        {
            using ( SqlConnection connection = new SqlConnection(GetConnectionString()) )
            {
                if ( connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                SqlDataAdapter adapter = new SqlDataAdapter(SQLCommand, connection);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "ds");
                connection.Close();
                return ds;
            }
        }

        /// <summary>
        /// 执行SQL语句并返回包含查询结果的DataReader
        /// </summary>
        /// <param name="SQLCommand">所执行的SQL命令</param>
        /// <param name="parameters">参数集合</param>
        /// <returns></returns>
        public static SqlDataReader ExecuteSQLDataReader ( string SQLCommand, SqlParameter[] parameters )
        {
            using ( SqlConnection connection = new SqlConnection(GetConnectionString()) )
            {
                SqlCommand command = new SqlCommand(SQLCommand, connection);
                for ( int i = 0; i < parameters.Length; i++ )
                    command.Parameters.Add(parameters[i]);
                if ( connection.State == ConnectionState.Closed )
                    connection.Open();
                SqlDataReader dataReader = command.ExecuteReader();
                connection.Close();
                return dataReader;
            }
        }
    }
}